import os
import sys
import datetime
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By 
from selenium.webdriver.firefox.service import Service as FirefoxService 
from selenium.webdriver.common.action_chains import ActionChains 
class likesShopTset():
    def __init__(self) -> None:

        driver_path =os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                                "geckodriver_exe")
        service = FirefoxService(excutable_path=driver_path) 
        options = webdriver.FirefoxOptions()
        self.brower = webdriver.Firefox(service=service,options=options)
        
    def tset1(self):
        self.brower.get('http://likeshop.abc/admin/account/login.html')

        sleep(3)

        #定位用户名输入并输入用户名
        self.brower.find_element(By.NAME,'account').send_keys('admin')

        #定位密码框并输入密码
        self.brower.find_element(By.NAME,'password').send_keys('1234')

        #定位验证码并输入密码
        self.brower.find_element(By.NAME,'code').send_keys('1111' )

        #定位登录按钮并点击登录按钮
        self.brower.find_element(By.ID,'login').click()
        sleep(3)

         # 点击商品
        self.brower.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)

        # 点击frame子框架
        iframe1 =self.brower.find_element(By.XPATH,'/html/body/div/div[3]/div[2]/iframe')
        #进入子框架
        self.brower.switch_to.frame(iframe1)
        #定位商品名称输入框输入商品名称
        self.brower.find_element(By.XPATH,'//[@id="keyword"]').send_keys('文具')

        self.brower.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/button[1]').click()

        sleep(1)
        # 获取列表
         # 获取列表
        tr_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if len(tr_list) == 0:
    # 如果列表为空，则没有找到任何元素
            print("没有找到任何 <tr> 元素")
        else:
    # 如果列表不为空，则至少找到了一个元素
            print(f"找到了 {len(tr_list)} 个 <tr> 元素")
       
        
        is_not_bug = True
        for item in tr_list:
            shop_name = item.find_element(By.XPATH,'./td[2]/div').text
            if '文具' not in shop_name:
                is_not_bug = False
        if is_not_bug:
            print('商品列表查询没有问题')
        else:
            print('商品列表有问题')
        sleep(1)
    
    # 提交图片    
    def test_upload_img(self, flie_path):
        # 提交文件
        self.browser.find_element(By.XPATH, "//input[@type='file']").send_keys(flie_path)
        sleep(5)
        self.browser.find_element(By.XPATH, '//*[@id="lists"]/li[1]/div/img').click()
        sleep(1)
        self.browser.find_element(By.XPATH, '//*[@id="used"]').click()
    
    def test_shop_add(self):
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(1)
        # 进入添加商品子框架
        shop_add_iframe = self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(shop_add_iframe)
        
        new = datetime.datetime.now().strftime("%d/%m/%Y,%H:%M:%S")
        
        # 输入商品名称
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('商品添加测试'+ new)
        
        # 选择第一个商品类别
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        
        # 选择第二个商品类别
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        
        # 选择第三个商品类别
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
 
        # 点击上传图片
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(1)
        
        # 进入图片上传子框架
        
        upload_iframe1 = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(upload_iframe1)
        self.test_upload_img('C:\Users\hinkintar\Desktop\软件测试\QQ图片20241104221007.jpg')    
        
        # 回到主框架
        self.browser.switch_to.default_content()
        frame1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        # 进入商品子框架
        self.browser.switch_to.frame(frame1)
        # 进入商品发布子框架
        self.browser.switch_to.frame(shop_add_iframe)
        
        # 点击商品轮播图 图片上传
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(3)
        # 进入商品轮播图 图片上传子框架
        upload_iframe2 = self.browser.find_element(By.XPATH, '/html/body/div[4]/div[2]/iframe')
        self.browser.switch_to.frame(upload_iframe2)
        self.test_upload_img('C:\Users\hinkintar\Desktop\软件测试\QQ图片20241104221007.jpg')
        sleep(2)
        
        # 回到主框架
        self.browser.switch_to.default_content()
        frame1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        # 进入商品子框架
        self.browser.switch_to.frame(frame1)
        # 进入商品发布子框架
        self.browser.switch_to.frame(shop_add_iframe)
        
        # 价格库存
        self.browser.find_element(By.XPATH, '/html/body/div[1]/ul/li[2]').click()
        sleep(2)
        # 填写市场价、价格、成本价、库存
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys("1")
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys("1")
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('500')
        
        # 回到主框架
        self.browser.switch_to.default_content()
        frame1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        
        # 进入商品子框架
        self.browser.switch_to.frame(frame1)
        
        # 点击保存
        self.browser.find_element(By.LINK_TEXT, '保存').click()
        sleep(2)
        
        # 上架
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[2]').click()
        sleep(2)
        
        # 获取列表
        shop_list = self.browser.find_elements(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if len(shop_list) != 0:
            # 点击上架
            self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[2]').click()
            sleep(2)
            # 点击确认
            self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
        

if __name__=='__main__':
    likeShopTset = likesShopTset()
    likeShopTset.tset1()
    likeShopTset.test_shop_add()